package com.yoomiito.app.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.yoomiito.app.Constant;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.model.config.ConfigInfo;

import cn.droidlover.xdroidmvp.cache.SharedPref;

public class UserInfoConfig {

    public static void putInviteCode(String inviteCode) {
        SharedPref.getInstance(App.getInstance()).putString(Constant.INVITE_CODE, inviteCode);
    }

    /**
     * 邀请码
     *
     * @return
     */
    public static String getInviteCode() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            return SharedPref.getInstance(App.getInstance()).getString(Constant.INVITE_CODE, "");//邀请码默认为空
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getInvitation_code();
    }

    public static ConfigInfo getConfigInfo() {
        if (App.mAppConfig == null ||
                App.mAppConfig.mConfigInfo == null) {
            String config = SharedPref.getInstance(App.getInstance()).getString(Constant.Config, "");
//            ConfigInfo configInfo = JSON.parseObject(oldConfig,ConfigInfo.class);
            if (!TextUtils.isEmpty(config)) {
                return JSON.parseObject(config, ConfigInfo.class);
            }
        }
        return App.mAppConfig.mConfigInfo;
    }

    public static void putCompanyId(int companyId) {
        SharedPref.getInstance(App.getInstance()).putInt(Constant.CompanyId, companyId);
    }

    public static int getCompanyId() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            return SharedPref.getInstance(App.getInstance()).getInt(Constant.CompanyId, 1);//1 默认千年艾
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getCompany_id();
    }

    public static void putRoleId(int roleId) {
        SharedPref.getInstance(App.getInstance()).putInt(Constant.RoleId, roleId);
    }

    public static int getRoleId() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            return SharedPref.getInstance(App.getInstance()).getInt(Constant.RoleId, 0);//默认0 最低级别
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getRole_id();
    }

    public static void putRelationId(String relationId) {
        SharedPref.getInstance(App.getInstance()).putString(Constant.RelationId, relationId);
    }

    public static String getRelationId() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            return SharedPref.getInstance(App.getInstance()).getString(Constant.RelationId, "");
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getRelation_id();
    }

    public static boolean IsAgent() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            return SharedPref.getInstance(App.getInstance()).getBoolean(Constant.IsAgent, false);
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getRegion_agent() == 1 ? true : false;
    }

    public static int getAgent() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            return SharedPref.getInstance(App.getInstance()).getInt(Constant.Agent, 0);
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getRegion_agent();
    }

    public static void putAgent(int agent) {
        SharedPref.getInstance(App.getInstance()).putInt(Constant.Agent, agent);
        if (agent == 1) {
            SharedPref.getInstance(App.getInstance()).putBoolean(Constant.IsAgent, true);
        } else {
            SharedPref.getInstance(App.getInstance()).putBoolean(Constant.IsAgent, false);
        }
    }

    private static void putUserConfig(String token, boolean isLogin) {
        SharedPref.getInstance(App.getInstance()).putBoolean(Constant.IS_LOGIN, isLogin);
        putToken(token);
    }


    public static void putToken(String token) {
        if (!TextUtils.isEmpty(token)) {
            SharedPref.getInstance(App.getInstance()).putString(Constant.TOKEN, token);
        }
    }


    public static void putUser(UserInfo userInfo) {
        String u = JSON.toJSONString(userInfo);
        SharedPref.getInstance(App.getInstance()).putString(Constant.UserInfo, u);
    }

    public static long getUserId() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            UserInfo userInfo = getUser();
            if (userInfo != null) {
                return userInfo.getId();
            }else {
                return 0;
            }
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getId();
    }

    public static String getTel() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            UserInfo userInfo = getUser();
            if (userInfo != null) {
                return userInfo.getMobile();
            }else {
                return "";
            }
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.getMobile();
    }

    public static boolean havePayPsw() {
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            UserInfo userInfo = getUser();
            if (userInfo != null) {
                return userInfo.isWithdraw_pass();
            }else {
                return false;
            }
        }
        return App.mAppConfig.mCurrentUser.mUserInfo.isWithdraw_pass();
    }

    public static boolean bindWX() {//true 未绑定微信
        if (App.mAppConfig == null
                || App.mAppConfig.mCurrentUser == null
                || App.mAppConfig.mCurrentUser.mUserInfo == null) {
            UserInfo userInfo = getUser();
            if (userInfo != null) {
                return TextUtils.isEmpty(userInfo.getOpen_id());
            }else {
                return false;
            }
        }
        return TextUtils.isEmpty(App.mAppConfig.mCurrentUser.mUserInfo.getOpen_id());
    }

    public static UserInfo getUser() {
        String u = SharedPref.getInstance(App.getInstance()).getString(Constant.UserInfo, "");
        if (!TextUtils.isEmpty(u)) {
            return JSON.parseObject(u, UserInfo.class);
        }
        return null;
    }

    /**
     * 登录信息
     *
     * @param userInfo
     */
    public static void loginPutUserConfig(UserInfo userInfo, boolean isLogin) {
        App.mAppConfig.mCurrentUser.mUserInfo = userInfo;
        App.mAppConfig.mCurrentUser.setCompanyId(userInfo.getCompany_id());
        App.mAppConfig.mCurrentUser.setRoleId(userInfo.getRole_id());
        putUser(userInfo);
        if (isLogin) putUserConfig(userInfo.getToken(), true);
        putCompanyId(userInfo.getCompany_id());
        putRoleId(userInfo.getRole_id());
        putInviteCode(userInfo.getInvitation_code());
        putRelationId(userInfo.getRelation_id());
        putAgent(userInfo.getRegion_agent());
    }

    /**
     * 公司id 角色id
     */
    public static void updateRoleId(UserInfo userInfo) {
        putUser(userInfo);
        if (App.mAppConfig != null && App.mAppConfig.mCurrentUser != null) {
            if (App.mAppConfig.mCurrentUser.getRoleId() != userInfo.getRole_id()) {//角色有变化 则重新储存
                App.mAppConfig.mCurrentUser.setRoleId(userInfo.getRole_id());
                putRoleId(userInfo.getRole_id());
            } else {//角色没有存储 则存储
                int roleId_1 = SharedPref.getInstance(App.getInstance()).getInt(Constant.RoleId, -1);
                if (roleId_1 == -1) {
                    putRoleId(userInfo.getRole_id());
                }
            }
        } else {
            putRoleId(userInfo.getRole_id());
        }

        if (TextUtils.isEmpty(getRelationId())) {
            putRelationId(userInfo.getRelation_id());
        }

        int companyId_1 = SharedPref.getInstance(App.getInstance()).getInt(Constant.CompanyId, -1);
        if (companyId_1 == -1) {
            putCompanyId(userInfo.getCompany_id());
        }

        if (!IsAgent()) {
            putAgent(userInfo.getRegion_agent());
        }

    }
}
